package com.assistant.controller;

import com.assistant.service.es.ElasticOperatorService;
import com.assistant.vo.resp.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/es",produces={"application/json;charset=UTF-8"})
@Slf4j
public class EsController {

    @Autowired
    private ElasticOperatorService elasticOperatorService;

    @PutMapping(value = "/create/index" )
    public AjaxResult createIndex(String className){
        try {
            Class<?> indexClass = Class.forName(className);
            elasticOperatorService.indexCreate(indexClass);
        } catch (ClassNotFoundException e) {
            return AjaxResult.error("索引类不存在");
        }
        return AjaxResult.success();
    }

    @DeleteMapping(value = "/delete/index")
    public AjaxResult deleteIndex(String index){
        elasticOperatorService.indexDelete(index);
        return AjaxResult.success();
    }
}
